<?php

// 关键词
class KeywordsAction extends Action {
	
	private $cLogs = null;
	private $cPlantform = null;
	
	function __construct(){
		//判断管理员登录状态
		admin_login_check();

        import('@.ORG.Get');
        import('@.ORG.Post');
		import('@.ORG.Plantform');
		import('@.ORG.Logs');
		
		$this->cPlantform 	= new Plantform();
		$this->cLogs 		= new Logs();
	}
	
	//列表
    public function index(){

    	admin_role('Keywords:index');

        $get = get(
            array(
                array('plantform_id'	,'intval',0),
                array('search'		    ,'trim'	,''),
                array('keywords'	    ,'trim'	,''),
                array('p'	    ,'intval'	,1)
            )
        );

        $plantform_id   = $get['plantform_id'];
        $search         = $get['search'];
        $search_keywords= $get['keywords'];
        $page           = $get['p'];
        $pageSize 	    = 100;

        $search_items = array(
            'url'				=>'keywords.url',
            'remark'		    =>'keywords.remark'
        );

        $where = array();
        if(array_key_exists($search, $search_items)){
            $where = array(
                $search_items[$search] => $search_keywords
            );
        }

        if($plantform_id > 0){
            $where['plantform_id'] = $plantform_id;
        }

    	import('@.ORG.Keywords');
    	$cKeywords = new Keywords();

        $allCount = 0;
    	$keyWords = $cKeywords->list_all($where,$page,$pageSize,$allCount);


        /**
        +------------------------------------------------------------------------------
         * 分页
        +------------------------------------------------------------------------------
         */
        //导入分页类
        import("@.ORG.Page");

        $pageParams = array(
            'g'		=> 'Admin',
            'plantform_id'  => $plantform_id,
            'search'        => $search,
            'keywords'      => $search_keywords
        );
        // 实例化分页类
        $p = new Page($allCount,$pageSize,$pageParams);

        // 分页显示输出
        $pager = $p->show();

        /**
        +------------------------------------------------------------------------------
         * 分页
        +------------------------------------------------------------------------------
         */


        //平台
        $plantforms = $this->cPlantform->lists();

        //搜索条件
        $keywords_search = C('KEYWORDS_SEARCH');

    	$this->assign(
    		array(
    			'APP_NAME'	=> APP_NAME,
                'keywords'	=> $keyWords,
                'search_keywords'	=> $search_keywords,
                'search'    => $search,
                'plantforms'	=> $plantforms,
                'plantform_id'	=> $plantform_id,
                'keywords_search'=> $keywords_search,
                'pager'         => $pager,
    			'MainPage'	=> 'Keywords:index'
    		)
    	);
    	$this->display(C('ADM_TEMPLATE'));
    }
    
    public function add(){
    	
    	admin_role('Keywords:add');
    	
    	$plantforms = $this->cPlantform->lists();
    	
    	$this->assign(
    		array(
    			'APP_NAME'	=> APP_NAME,
    			'plantforms'=> $plantforms,
    			'MainPage'	=> 'Keywords:add'
    		)
    	);
    	$this->display(C('ADM_TEMPLATE'));
    }
    
    public function add_do(){

    	admin_role('Keywords:add');
    	
    	$plantform_id	= $this->_post('plantform_id'	,'intval',0);
    	$url 			= $this->_post('url'			,'','');
    	$title 			= $this->_post('title'			,'','');
    	$keywords	 	= $this->_post('keywords'		,'','');
        $description 	= $this->_post('description'	,'','');
        $remark 	    = $this->_post('remark'	        ,'trim','');
    	$disabled 		= $this->_post('disabled'		,'intval',0);
        $sort    	    = $this->_post('sort'	        ,'intval',0);

 		import('@.ORG.Keywords');
    	$cKeywords = new Keywords();
    	
    	//已存在
    	$find = $cKeywords->exist($plantform_id,$url);
    	
    	if($find){
    		json_error('URL在该平台已经存在，添加失败');
    	}else{    	
    		$add_result = $cKeywords->add($plantform_id,$url,$title,$keywords,$description,$remark,$disabled,$sort);
    	}
    	
    	if($add_result){
    		
    		$this->cLogs->add('Keywords:add','下单URL添加成功:'.$url);
    		
    		json_success('URL添加成功');
    	}else{
    		    		
    		json_error('URL添加失败');
    	}
		
    }
    
    //详情或修改
    public  function detail(){
    	
    	admin_role('Keywords:edit');
    	
    	$plantforms = $this->cPlantform->lists();
    	
    	$id 	= $this->_get('id'	,'',0);
				
 		import('@.ORG.Keywords');
    	$cKeywords = new Keywords();
    	
    	//单个详情
    	$keywords = $cKeywords->get_by_id($id);

    	$this->assign(
    		array(
    			'APP_NAME'		=> APP_NAME,
    			'keywords'		=> $keywords,
    			'plantforms'	=> $plantforms,
    			'MainPage'		=> 'Keywords:detail'
    		)
    	);
    	
    	$this->display(C('ADM_TEMPLATE'));
    }
    
    public function update(){
    	
    	admin_role('Keywords:edit');
    	
    	$id 			= $this->_post('id'				,'','0');
    	$url 			= $this->_post('url'			,'','');
    	$title 			= $this->_post('title'			,'','');
    	$keywords	 	= $this->_post('keywords'		,'','');
        $description 	= $this->_post('description'	,'','');
        $remark 	    = $this->_post('remark'	        ,'trim','');
    	$disabled 		= $this->_post('disabled'		,'intval',0);
        $sort    	    = $this->_post('sort'	        ,'intval',0);
    
 		import('@.ORG.Keywords');
    	$cKeywords = new Keywords();
    	
    	$upItems = array(
    		'url'			=> $url,
    		'title'			=> $title,
    		'keywords'		=> $keywords,
    		'description'	=> $description,
            'remark'        => $remark,
            'disabled'		=> $disabled,
            'sort'  		=> $sort
    	);
    	$update_result = $cKeywords->update($id,$upItems);
    	
    	json_out($update_result);
    }

    /**
     * 打乱，随机排序
     */
    function random_sort(){

        admin_role('Keywords:index');

        import('@.ORG.Keywords');
        $cKeywords = new Keywords();

        //列表
        $keyWords = $cKeywords->lists();

        $keyWords_count = count($keyWords);

        $numbers = range(0, $keyWords_count-1);

        //打乱数组
        shuffle($numbers);

        $sort_change = 0;
        foreach ($numbers as $i=>$n) {

            $id = $keyWords[$i]['id'];
            $upItems = array(
                'sort'  		=> $n
            );
            $update_result = $cKeywords->update($id,$upItems);

            if($update_result){
                $sort_change++;
            }
        }

        if($sort_change){
            json_success('共:'.$sort_change.'个URL已重新排序');
        }else{
            json_success('重新排序失败');
        }
    }

    /**
     * 修改启动状态
     */
    public function update_disabled(){

        admin_role('Keywords:edit');

        $id 			= $this->_post('id'				,'','0');
        $disabled 		= $this->_post('disabled'		,'intval',0);

        import('@.ORG.Keywords');
        $cKeywords = new Keywords();

        $upItems = array(
            'disabled'		=> $disabled
        );
        $update_result = $cKeywords->update($id,$upItems);

        json_out($update_result);
    }

    public function batch_update_disabled(){

        admin_role('Keywords:edit');

        $post = post(array(
            array('keywords',		'trim'		,''),
            array('disabled',		    'intval'	,'0'),
        ));

        $keywords_id 	= $post['keywords'];
        $disabled 	    = $post['disabled'];

        if($keywords_id != ''){

            import('@.ORG.Keywords');
            $cKeywords = new Keywords();
            $aft_rows = $cKeywords->batch_update_disabled($keywords_id,$disabled);

            if($aft_rows > 0){

                json_success($aft_rows.'个URL修改成功,');
            }else{
                json_error('下单URL操作失败');
            }
        }else{
            json_error('check_keywords参数错误，请检查');
        }
    }
}